package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.mapper.PermissionMapper;
import com.hamster.crm.mapper.UserMapper;
import com.hamster.crm.pojo.Permission;
import com.hamster.crm.pojo.User;
import com.hamster.crm.pojo.dto.DepartmentUserList;
import com.hamster.crm.service.UserService;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class UserServiceImpl  implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    //    根据账号phone查询user对象
    @Override
    public User findUserByPhone(String phone){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user2 = userMapper.selectOne(queryWrapper);
        if (user2 == null){
            return null;
        }
        String id = user2.getId();
        List<String> permissionIdList = userMapper.queryPermissionIdByUserId(id);
        if (CollectionUtils.isEmpty(permissionIdList)){
            return null;
        }
        //根据id集合查查询权限列表
        List<Permission> permissionList = permissionMapper.selectBatchIds(permissionIdList);

        user2.setPermissionList(permissionList);
        return user2;
    }


    //    新增用户
    @Override
    public void addUser(User user) {
        String password = user.getPassword();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //使用BCrypt加密，每次加密使用一个随机盐
        String encode = bCryptPasswordEncoder.encode(password);
        user.setPassword(encode);

        userMapper.insert(user);
    }

    //    获取用户列表
    @Override
    public List<User> getUserList() {
        return userMapper.queryUserList();
    }

    /**
     * 角色关联用户需要，user 的列表
     * @return
     */
    @Override
    public List<DepartmentUserList> listUsersSelect() {

        return userMapper.listUsersSelect();
    }

    /**
     * 更新 用户内容
     * @param user
     */
    @Override
    public void updateUser(User user) {
        if (StringUtils.isBlank(user.getPassword())){
            user.setPassword(null);
        }else {
            String password = user.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            //使用BCrypt加密，每次加密使用一个随机盐
            String encode = bCryptPasswordEncoder.encode(password);
            user.setPassword(encode);
        }
        userMapper.updateById(user);
    }

    /**
     * 删除用户
     * @param id 用户id
     */
    @Override
    public void deleteUser(String id) {
        userMapper.deleteById(id);
    }

    /**
     * 根据 用户名 条件查询
     * @param phone         用户名
     * @param currentPage   当前页号
     * @param pageSize      每页条数
     * @return
     */
    @Override
    public PageResult<User> listSchemeResults(String phone, Integer currentPage, Integer pageSize) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("id","phone","name","department","position");
        wrapper.orderByDesc("id");
        //根据任务名称过滤
        if (StringUtils.isNotBlank(phone)){
            wrapper.like("phone","%"+phone+"%");
        }
        IPage pageList = userMapper.selectPage(new Page<User>(currentPage, pageSize), wrapper);
        Long pages = pageList.getPages();
        List<User> currentList =  pageList.getRecords();
        return new PageResult<User>(pageList.getTotal(),pages.intValue(),currentList);
    }

}